package com.client.forms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.client.controller.NotNullTextBoxController;
import com.client.service.data.CompagnieDataService;
import com.client.service.data.CompagnieDataServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.googlecode.objectify.Key;
import com.shared.model.Compagnie;
import com.shared.model.Produit;
import com.shared.model.Tarification;

public class ProduitEditor extends DataEditor<Produit>{
	
	
	private TextBox nomTextBox = new TextBox();
	private TextBox typeTextBox = new TextBox();
	private ListBox nomCompagnieTextBox = new ListBox();
	private TarificationEditor tarificationEditor = new TarificationEditor();
	private List<Long> compagnieId = new ArrayList<Long>();
	
	public  final CompagnieDataServiceAsync compagnieDataService = GWT.create(CompagnieDataService.class);
	
	public ProduitEditor() {
		edited = new Produit();
		setSpacing(5);
		
		Label labelRens = new Label("Description");		
		labelRens.setStyleName("titre") ;
		add(labelRens);
		
		HorizontalPanel nomPanel = new HorizontalPanel();
		nomPanel.add(new Label("Nom : "));
		nomPanel.add(nomTextBox);
		add(nomPanel);
		listController.add(new NotNullTextBoxController(nomTextBox, "Nom Tarif"));
		
		HorizontalPanel typePanel = new HorizontalPanel();
		typePanel.add(new Label("Type : "));
		typePanel.add(typeTextBox);
		add(typePanel);
		listController.add(new NotNullTextBoxController(typeTextBox, "Type Contrat"));
		
		HorizontalPanel nomCompagniePanel = new HorizontalPanel();
		nomCompagniePanel.add(new Label("Nom Compagnie : "));
		
		compagnieDataService.getList(new AsyncCallback<List>() {
			
			public void onSuccess(List result) {
				for(Compagnie c :(List<Compagnie>) result){					
					nomCompagnieTextBox.addItem(c.getNom());
					compagnieId.add(c.getKey());					
				}
				
			}
			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());				
			}
		});
		
		
		nomCompagniePanel.add(nomCompagnieTextBox);
		add(nomCompagniePanel);
		Label labelTarif = new Label("Tarif");		
		labelTarif.setStyleName("titre") ;
		add(labelTarif);		
		add(tarificationEditor);	
		
		setEmpty();
	}
	
	
	public void fill(){
		if(edited!=null){
			nomTextBox.setValue(edited.getNom());
			typeTextBox.setValue(edited.getTypeContrat());
			tarificationEditor.setEdited(edited.getTarification());
			tarificationEditor.fill();
		}
	}
	
	@Override
	public Produit getEdited() throws IllegalArgumentException{
		
		edited.setNom(nomTextBox.getValue());
		edited.setTypeContrat(typeTextBox.getValue());
		edited.setTarification((Tarification) tarificationEditor.getEdited());
		edited.setCompagnie(new Key<Compagnie>(Compagnie.class, compagnieId.get(nomCompagnieTextBox.getSelectedIndex())));			
		return edited;
	}

	@Override
	public Map<String, String> getEditedAsMap() throws IllegalArgumentException {
		check();
		Map<String,String> result = new HashMap<String, String>();
		result.put("nom", nomTextBox.getValue());
		result.put("typeContrat", typeTextBox.getValue());
		result.put("nomCompagnie", nomCompagnieTextBox.getItemText(nomCompagnieTextBox.getSelectedIndex()));
		result.putAll(tarificationEditor.getEditedAsMap());
		return result;
	}
	
	public String getNomCompagnie(){
		return nomCompagnieTextBox.getItemText(nomCompagnieTextBox.getSelectedIndex());
	}


	@Override
	public void setEmpty() {
		setEdited(new Produit());
		fill();
		tarificationEditor.setEmpty();
		
	}

	

}
